package com.example.yoga.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yoga.entity.*;
import com.example.yoga.service.ICardCourseService;
import com.example.yoga.service.ICardService;
import com.example.yoga.service.ICoursePriceService;
import com.example.yoga.service.IUserCardService;
import com.example.yoga.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenkai
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/coursePrice")
public class CoursePriceController {

    @Autowired
    private ICoursePriceService coursePriceService;

    @Autowired
    private IUserCardService userCardService;

    @Autowired
    private ICardService cardService;

    @Autowired
    private ICardCourseService cardCourseService;
    /**
     * 查询课程单价，回显到界面
     * @param courseid
     * @return
     */
    @RequestMapping("/getPriceById")
    public JsonResult<CoursePrice> getPriceById(Integer courseid) {
        System.out.println(courseid+"课程id");
        //根据课程id获取单价
        QueryWrapper<CoursePrice> coursePriceQueryWrapper = new QueryWrapper<>();
        coursePriceQueryWrapper.eq("courseId",courseid);
        CoursePrice one = coursePriceService.getOne(coursePriceQueryWrapper);
        System.out.println("单价列表"+one);
        return new JsonResult<>(2000,one) ;
//        QueryWrapper<User> wrapper = new QueryWrapper<User>();
//        wrapper.eq("roleId", Id);
//        List<User> userList = userService.list(wrapper);
//        if (userList.size()>0){
//            return new JsonResult<>(2000,userList) ;
//        }else{
//            return new JsonResult<>(4000,"该角色下没有用户") ;
//        }

    }

    /**
     * buyCourseById  通过课程id进行购买课程的操作
     */
    @RequestMapping("/buyCourseById")
    public JsonResult<Void> buyCourseById(Integer courseid,Integer userid,Double price){
        System.out.println("课程id"+courseid+"********用户id"+userid+"******课程单价"+price);
        //1.找出用户的卡id
        QueryWrapper<UserCard> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userid);
        UserCard one = userCardService.getOne(wrapper);
        if (one==null){
            return new JsonResult<>(4000,"你还没有办卡，请办卡之后再来！");
        }
        //找卡
        Card card = cardService.getById(one.getCardid());
        //2.找到卡之后,看是否已经购买过该课程
        QueryWrapper<CardCourse> purchasedWrapper = new QueryWrapper<>();
        purchasedWrapper.eq("cardId",card.getCardid());
        purchasedWrapper.eq("courseId",courseid);
        CardCourse Is_exist = cardCourseService.getOne(purchasedWrapper);
        if (Is_exist!=null){
            return new JsonResult<>(4000,"您已经购买过，请勿重复购买！");
        }
        // 进行购买操作
            //判断卡内余额是否大于课程价格
          if(card.getBalance().doubleValue()<price){
              return new JsonResult<>(4000,"余额不足，请充值！");
          }
//        BigDecimal decimal = new BigDecimal(price);
        BigDecimal Balance = card.getBalance().subtract(BigDecimal.valueOf(price));
        card.setBalance(Balance);
        //条件构造器
        QueryWrapper<Card> cardQueryWrapper = new QueryWrapper<>();
        cardQueryWrapper.eq("cardId",card.getCardid());
        //更新数据
        cardService.update(card,cardQueryWrapper);
        //卡内余额已更新，更新完之后，增加一条购买课程的记录（记录卡id和课程id）

        CardCourse cardCourse = new CardCourse();
        cardCourse.setCardid(card.getCardid());
        cardCourse.setCourseid(courseid);
        cardCourse.setBuydate(new Date());
        boolean b = cardCourseService.save(cardCourse);
        if (b){
            return new JsonResult<>(2000,"购买成功！");
        }
        return new JsonResult<>(4000,"购买失败！");
    }



}

